package com.cnpinxi.service;

import com.cnpinxi.dao.ProductSerialMapper;
import com.cnpinxi.domain.ProductSerial;
import com.cnpinxi.log.LogFactory;
import com.cnpinxi.service.*;
import com.cnpinxi.util.GenerateID;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AdminSerialServiceImpl implements com.cnpinxi.service.AdminSerialService {
    protected Logger logger = LogFactory.getLogger(this.getClass());
    @Autowired
    ProductSerialMapper productSerialMapper;

    @Override
    public int insertProductSerial(Map<String, String> map) {
        try{
            int serialNum = Integer.parseInt(map.get("serialNumber")) ;
            long productEntityId = Long.parseLong(map.get("productEntityId"));
            ProductSerial productSerial = null;
            for (int i=0;i<serialNum;i++){
                productSerial = new ProductSerial();
                productSerial.setProductEntityId(productEntityId);
                productSerial.setSerialNumber(GenerateID.getRandom(16,false,"abcdefghigklmnopqrstuvwxyz"));
                productSerial.setStauts(Byte.parseByte("0"));
               // productSerial.setCreateTime(new Date());
               //productSerial.setUserId(Long.parseLong(map.get("adminUserId")));
                productSerialMapper.insertProductSerial(productSerial);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return 1;
    }

    /**
     * 查询序列号列表
     * @param map
     * @return
     */
    @Override
    public List<ProductSerial> selectProductSerial(Map<String, String> map) {
        return productSerialMapper.selectProductSerial(map);
    }

    /**
     * 更改序列号状态
     * @param record
     * @return
     */
    @Override
    public int updateSerialStatus(ProductSerial record) {
        return productSerialMapper.updateSerialStatus(record);
    }
}
